﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Xml.Serialization;
using System.Xml;

namespace TestSys.Models
{
    public class DataManager
    {
        private dbEntities _entity;
        public dbEntities entity
        {
            get
            {
                return _entity;
            }
        }
        public DataManager()
        {
            _entity = new dbEntities();
        }

        //db operations
        public generalinfo GetUser(string Login)
        {
            var genInfo = (from item in entity.generalinfo where item.Login == Login select item).FirstOrDefault();
            return genInfo;
        }

        public userinfo GetCurrentUser()
        {
            throw new NotImplementedException();
        }

        public admininfo GetCurrentAdmin(System.Security.Principal.IPrincipal User)
        {
            admininfo currAdm = (from item in entity.admininfo where item.generalinfo.Login == User.Identity.Name select item).FirstOrDefault();
            return currAdm;
        }

        public tests GetTestByID(int id)
        {
            var res = (from item in entity.tests where item.TestID == id select item).FirstOrDefault();
            return res;
        }

        public courses GetCourseByID(int id)
        {
            var res = (from item in entity.courses where item.CourseID == id select item).FirstOrDefault();
            return res;
        }

        public bool SubmitNewResult(Int64 currentUser, int TestID, int result)
        {
            userinfo currUsr = entity.userinfo.Select((i)=>i).Where((item)=> item.UID==currentUser).FirstOrDefault();
            if(currUsr==null) return false;
            currUsr.testsresults.Add(new testsresults() { PassingDate = DateTime.Now, Result = result, TestID = TestID });
            entity.SaveChanges();
            return true;
        }
        
        //xml operations
        public List<rootQuestion> LoadTest(int id)
        {
            tests test = GetTestByID(id);
            string path = HttpContext.Current.Server.MapPath(test.TestPath);
            if (test != null && File.Exists(Path.GetFullPath(path)))
            {
                XmlSerializer xs = new XmlSerializer(typeof(root));
                FileStream fs = new FileStream(path, FileMode.Open);
                XmlReader xr = new XmlTextReader(fs);
                root questions = (root)xs.Deserialize(xr);
                fs.Close();
                return questions.Question.ToList();
            }
            return null;
        }

        //test cheking
        public int CheckTest(string[] corr_ans/*correct*/, string[] users_ans/*users*/)
        {
            int result = 0;
            for (int i = 0; i < corr_ans.Length; i++)
                if (corr_ans[i] == users_ans[i])
                    result++;
            return result;
        }
    }
}